import requests
import json


# 登录界面
def login():
    url = "http://admin.prokash.xyz/v2/login"
    data = {
        "account": "cadmin",
        "password": "666666"
    }
    # json参数会将字典类型自动转为json数据
    response = requests.post(url, json=data)
    print(response.status_code)
    # 验证HTTP状态码是否为200
    if response.status_code == 200:
        # 提取响应结果中的token字段
        token = json.loads(response.text)["data"]["token"]
        print(token)
        return token
    else:
        print(f"接口请求失败，状态码：{response.status_code}")
        return None


# 催收界面
def use_token(token):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0",
        # 不同产品不同
        "Referer": "http://admin.prokash.xyz/",
        # 不同产品不同
        "Host": "admin.prokash.xyz",
        "lang": "ZH",
        "Accept": "application/json, text/plain, */*",
        # 关键token，记录登录唯一标识
        "Authorization": f"Bearer {token}"
    }

    params = {
        # 分配开始时间
        "startTime": "2024-02-05 00:00:00",
        # 分配结束时间
        "endTime": "2024-02-11 23:59:59",
        "page": 1,
        "limit": 100,
    }
    url = "http://admin.prokash.xyz/v2/collection/performance/getCollectEmployeePerformance"

    resp = requests.get(url, params=params, headers=headers)
    # print(resp.text)
    # print(json.loads(resp.text))
    print(resp.status_code)
    # json.loads()将json字符串转为python对象，json.dumps()将python对象转为json字符串
    datas = json.loads(resp.text)["data"]
    print(datas)
    print(
        "阶段" + "\t" + "催收员" + "\t" + "总分配单数" + "\t" + "总分配金额" + "\t" + "总催回单数" + "\t" + "全还款催回金额" + "\t" + "部分催回单数" + "\t" + "部分催回金额" + "\t" + "展期订单数" + "\t" + "展期金额" + "\t" + "总催回金额")

    for data in datas:
        # PT-S-1   PT-S0   PT-S1  PT-S2  PT-S3  PT-S4
        # if data["stageName"] == "PT-S-1":
        # 催收阶段（加纳是催收组）
        csjd = data["stageName"]
        # 催收员
        csy = data["receiverName"]
        # 分配订单数
        fpdds = data["orderNumber"]
        # 分配金额
        fpje = data["orderAmount"]
        # 全部催回单数
        qbchds = data["successOrder"]
        # 全部催回金额
        qbchje = data["successAmount"]
        # 部分催回单数
        bfchds = data["portOrder"]
        # 部分催回金额
        bfchje = data["portAmount"]
        # 展期订单数
        zqdds = data["rolloverOrder"]
        # 展期金额
        zqje = data["rolloverAmount"]
        # 总催回金额
        zchje = qbchje + bfchje + zqje
        # 总催回率（订单数）
        # qbchl = qbchds / fpdds
        # 总催回率（金额）
        # qbchl2 = zchje / fpje
        print(
            csjd + "\t" + csy + "\t" + f'{fpdds}' + "\t" + f'{fpje}' + "\t" + f'{qbchds}' + "\t" + f'{qbchje}' + "\t" + f'{bfchds}' + "\t" + f'{bfchje}' + "\t" + f'{zqdds}' + "\t" + f'{zqje}' + "\t" + f'{zchje}')


token = login()
use_token(token)
